extends CharacterBody2D
class_name Player

@onready var buff_manager: BuffManager = %BuffManager
@export var stats: PandoraEntity
@onready var weapon_root: Node2D = %WeaponRoot
@onready var visuals: CanvasGroup = %Visuals

@onready var player_animations: PlayerAnimations = %PlayerAnimations
@onready var player_state: PlayerState = %PlayerState
@onready var player_effect: Node = %PlayerEffect
@onready var authority_aura: Node2D = %AuthorityAura

var player_stats: PandoraEntity

var weapon = null
var knockback_speed = 0 # 后坐力速度s
var direction: Vector2

var timer: Timer
var time = 60
var _elapsed = 0 

func _init() -> void:
	player_stats = PandoraEntity.new()

func _ready():
	player_stats = stats.instantiate()
	weapon = Utils.weapon_list["1"].instantiate()
	PlayerDataManager.player_weapon_list[0] = weapon
	weapon.name = str(1)
	weapon_root.add_child(weapon)
	#local_weapon.setOwner(self)
	#weapon.set_use(true)
	Utils.player = self
	
func _physics_process(_delta):
	# 移动
	direction = Input.get_vector("left", "right", "up", "down")
	player_state.changeAnim(direction)
	player_animations.set_look_at(get_global_mouse_position())
	## 如果有武器
	#if weapon:
		#weapon.look_at(get_global_mouse_position())	
		
func playerWeaponListChange(_new_value):
	for weapon_id in PlayerDataManager.player_weapon_list:
		if !weapon_root.has_node(str(weapon_id)):
			var local_gun = PlayerDataManager.player_weapon_list[weapon_id] as BaseGun
			local_gun.name = str(weapon_id)
			weapon_root.add_child(local_gun)
			local_gun.setOwner(self)
			if weapon == null:
				weapon = local_gun
				weapon.set_use(true)
				
func changeWeapon(weapon_id):
	for item in weapon_root.get_children():
		item.set_use(item.name == str(weapon_id))
		
func cameraSnake(step):
	get_tree().call_group("camera", "shootShake", step)
	
func _on_检测遮挡_body_shape_entered(body_rid, body, _body_shape_index, _local_shape_index):
	#获取图块坐标
	if body is BaseEnemy or body is Player:
		return
	var cellCroods = body.get_coords_for_body_rid(body_rid)
	##获取图集坐标
	#var atlasCroods = body.get_cell_atlas_coords(2,cellCroods)
	##获取图集资源
	#var tileSetAtlasSource= body.tile_set.get_source(0) as TileSetAtlasSource
	##获取图集中图块的占用单元格尺寸
	#var size = tileSetAtlasSource.get_tile_size_in_atlas(atlasCroods)
	#获取图块材质
	var 材质 = body.get_cell_tile_data(cellCroods).material
	if 材质 != null:
		var shader = 材质 as ShaderMaterial
		shader.set_shader_parameter("alpha", 0.65)
	pass

func _on_检测遮挡_body_shape_exited(body_rid, body, _body_shape_index, _local_shape_index):
	if body is BaseEnemy:
		return
	var cellCroods = body.get_coords_for_body_rid(body_rid)
	var 材质 = body.get_cell_tile_data(cellCroods).material
	if 材质 != null:
		var shader = 材质 as ShaderMaterial
		shader.set_shader_parameter("alpha", 0)
	pass

## 进入关卡时恢复所有状态
func init_game():
	player_stats.set_float("current_hp", player_stats.get_float("max_hp"))
	player_stats.set_float("current_mp", player_stats.get_float("max_mp"))
	EventBus.push_event("show_time", [time, true])
	_elapsed = 0
	if timer != null:
		timer.queue_free()
	timer = Timer.new()
	timer.wait_time = 1
	timer.one_shot  = false
	timer.timeout.connect(_on_timer_timeout)
	add_child(timer)
	timer.start()
	
func _on_timer_timeout():
	_elapsed += 1
	if time - _elapsed < 0:
		EventBus.push_event("show_time", [0, false])
		Utils.show_fluent_tip("时间到,失败！")
		player_state.state_send_event("to_die")
	else:
		EventBus.push_event("show_time", [time - _elapsed, true])
	
## 获取视觉效果组件
func get_visuals() -> CanvasGroup:
	return visuals
